package com.example.woke;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

public class HtmlActivity extends Activity {

    private String url, urlname;
    private WebView mWebView = null;
    private Intent intent;
    //	public static String TEST_IMAGE;
    //	private static final String FILE_NAME = "/ic_icon2.jpg";
    private ClipData myClip;
    private ClipboardManager myClipboard;

    private Handler handler;
    private Dialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html);
        dialog= Util.createLoadingDialog(HtmlActivity.this);
        intent = getIntent();

        intvkew();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断是否可以返回操作
        if (mWebView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();

        } else {
            mWebView.destroy();
            finish();
        }
        return true;

    }


    //	private void initImagePath() {
    //		try {
    //			if (Environment.MEDIA_MOUNTED.equals (Environment
    //					.getExternalStorageState ())
    //					&& Environment.getExternalStorageDirectory ().exists ()) {
    //				TEST_IMAGE = Environment.getExternalStorageDirectory ()
    //						.getAbsolutePath () + FILE_NAME;
    //			} else {
    //				TEST_IMAGE = getApplication ().getFilesDir ().getAbsolutePath ()
    //						+ FILE_NAME;
    //			}
    //			// 创建图片文件夹
    //			File file = new File (TEST_IMAGE);
    //			if (! file.exists ()) {
    //				file.createNewFile ();
    //				Bitmap pic = BitmapFactory.decodeResource (getResources (),
    //						R.drawable.ic_icon2);
    //				FileOutputStream fos = new FileOutputStream (file);
    //				pic.compress (CompressFormat.JPEG, 100, fos);
    //				fos.flush ();
    //				fos.close ();
    //			}
    //		} catch (Throwable t) {
    //			t.printStackTrace ();
    //			TEST_IMAGE = null;
    //		}
    //	}

    @SuppressLint("JavascriptInterface")
    private void intvkew() {

        //		// 初始化图片路径
        //		new Thread () {
        //			public void run() {
        //				initImagePath ();
        //			}
        //		}.start ();

        findViewById(R.id.avhtml_image_back).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent(HtmlActivity.this,
//                        MainActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                startActivity(intent);
                finish();
            }
        });

        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        url = intent.getStringExtra("url");
        urlname = intent.getStringExtra("urlname");
        Log.e("urlname", "$" + url);
        TextView mTitel = (TextView) findViewById(R.id.avhtml_text_title);
        mTitel.setText(urlname);
        mWebView = (WebView) findViewById(R.id.webView11);
        WebSettings settings = mWebView.getSettings();
        setSettings(settings);
        mWebView.setWebViewClient(new MyWebViewClient());
        //		mWebView.loadUrl("http://www.88china.com");
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                mWebView.loadUrl(url);
            } else {
                mWebView.loadUrl("http://" + url);
            }
        }
    }
    private void setSettings(WebSettings settings) {
        settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        mWebView.setBackgroundColor(getResources().getColor(R.color.white)); // 设置背景色
        //		mWebView.getBackground().setAlpha(50); // 设置填充透明度 范围：0-255
        //		settings.setSupportZoom(false);// 用于设置webview放大
        //		settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);
        //		settings.setTextSize (TextSize.NORMAL);
        ////		settings.setPluginState(PluginState.ON);
        settings.setDatabaseEnabled(true);
    }


    // 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null) {
                Log.e("UrlLoading",url);
                //                Uri uri = Uri.parse(url);
                //                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                //                startActivity(intent);
                view.loadUrl(url);
            }
            return false;

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            mWebView.setVisibility(View.VISIBLE);
            Log.e("onPageFinishedurl......",url);
            Log.e("onPageFinishedgetUrl",view.getUrl());
            //            Log.e("getOriginalUrl()",view.getOriginalUrl());
            if(url.contains("status=00")||view.getUrl().contains("status=00")){
                handler = new Handler();
                // 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        Intent intent = new Intent(HtmlActivity.this,
                                MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    }
                }, 1000);
            }
            super.onPageFinished(view, url);
            Util.closeDialog(dialog);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);
            Log.e("onPageStarted",url);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(null, 1, "访问失败", null);
            Log.e("onReceivedError......",failingUrl);
        }
    }




}
